package com.yanqu.road.server.channelserver.manager.activity.washe;

import com.yanqu.road.dao.impl.channelcenter.ChannelActivityDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.helper.SeasonHelper;
import com.yanqu.road.server.TempMgr;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelWasheMgr extends TempMgr {

    // 渠道ID组成的key，活动
    private static final Map<String, ChannelWasheActivity> activityMap = new ConcurrentHashMap<>();

    public static synchronized ChannelWasheActivity getActivity(int channelId) {
        for (Map.Entry<String, ChannelWasheActivity> entry : activityMap.entrySet()) {
            if (SeasonHelper.matchWasheRangeKey(entry.getKey(), channelId)) {
                return entry.getValue();
            }
        }
        // 找不到活动，读库看看
        ActivityInfo activityInfo = new ChannelActivityDaoImpl().getOpenWasheActivityInfo(channelId, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
        if (activityInfo == null) {
            return null;
        }
        String key = SeasonHelper.getWasheRangeKey(activityInfo.getParam6());
        ChannelWasheActivity activity = new ChannelWasheActivity(activityInfo);
        activityMap.put(key, activity);
        return activity;
    }

    public static Map<String, ChannelWasheActivity> getActivityMap() {
        return activityMap;
    }

    public static synchronized void agentReloadConfig() {
        Set<String> activitySet = new HashSet<>();
        List<ActivityInfo> list = new ChannelActivityDaoImpl().getAllOpenWasheActivityInfo(ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
        for (ActivityInfo activityInfo : list) {
            String key = SeasonHelper.getWasheRangeKey(activityInfo.getParam6());
            if (activityMap.containsKey(key)) {
                ChannelWasheActivity activity = activityMap.get(key);
                activity.reloadConfig(activityInfo);
                activitySet.add(key);
            }
        }

        // 移除改范围前或者过期的活动
        for (Map.Entry<String, ChannelWasheActivity> entry : activityMap.entrySet()) {
            if (!activitySet.contains(entry.getKey())) {
                activityMap.remove(entry.getKey());
                getLogger().error("remove expire activity {} rangeKey {}.", entry.getValue().getConfig().getActivityInfo().getActivityId(), entry.getKey());
            }
        }
    }

    @Override
    public boolean save() {
        for (ChannelWasheActivity activity : activityMap.values()) {
            try {
                activity.save();
            } catch (Exception e) {
                getLogger().error("error : ", e);
            }
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
